package fristphase.day9_07.my_plane_battle;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class FlyingObject {
    public static int DEAD = 0;
    public static int LIVE = 1;
    public static int REMOVE = 2;
    public int state = LIVE;
    public int width,height,x,y,speed;
    FlyingObject(int width,int height){
        this.width = width;
        this.height = height;
        Random random = new Random();
        x = random.nextInt(400-width+1);
        y = -height;
        speed = 4;
    }
    FlyingObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }



    public abstract void step();
    public abstract ImageIcon getImage();


    public boolean getLive(){
        return state == LIVE;
    }
    public boolean getDead(){
        return state == DEAD;
    }
    public boolean getRemove(){
        return state == REMOVE;
    }
    public void goDead(){
        this.state = DEAD;
    }

    //越界判断
    public boolean enemyPlaneOutBoundOf(){
        return y>=World.HEIGHT || x>=World.WIDTH;
    }

    //碰撞判定
    public boolean isHit(FlyingObject other){
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        return x1 <= other.x && x2 >= other.x && y1 <= other.y && y2 >= other.y;
    }

    //减命设置
    public int enemyPlaneSubtractLife(){
        return 1;
    }

    public void paintImage(Graphics g){
        if(state != REMOVE){
            this.getImage().paintIcon(null,g,x,y);
        }
    }
//    public void paintBang(Graphics g){
//        if(state == REMOVE){
//            Images.bom1.paintIcon(null,g,x,y);
//        }
//    }
}
